package br.com.danieljr.crowdmap;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import br.com.danieljr.crowdmap.services.AlarmeColeta;

public class ConfiguracaoActivity extends ActionBarActivity {

	private SharedPreferences preferencias;
	CheckBox coletar;
	EditText minutos,raio;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		preferencias = getSharedPreferences("coleta", MODE_PRIVATE);
		setContentView(R.layout.activity_configuracao);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		coletar = (CheckBox) findViewById(R.id.ativarColeta);
		minutos = (EditText) findViewById(R.id.minutos);
		raio = (EditText) findViewById(R.id.raio);
		boolean ativado = preferencias.getBoolean("ativado", false);
		int min = preferencias.getInt("minutos", 5);
		int r = preferencias.getInt("raio", 200);
		if (!ativado) {
			coletar.setChecked(false);
		}
		minutos.setText(""+min);
		raio.setText(""+r);
		super.onStart();
	}
	
	public void ativarColeta(View v) {
		Editor editor = preferencias.edit();
		AlarmeColeta alarme = new AlarmeColeta(this);
		int min = Integer.parseInt(minutos.getText().toString());
		int r = Integer.parseInt(raio.getText().toString());
		if (!coletar.isChecked()) {
			alarme.desligarAlarme();
			editor.putBoolean("ativado", false);
			Toast.makeText(this, "Coleta desligada", Toast.LENGTH_LONG).show();
		} else {
			alarme.setMinutos(min);
			editor.putBoolean("ativado", true);
			Toast.makeText(this, "Coleta programada a cada "+min+" minutos", Toast.LENGTH_LONG).show();
		}
		editor.putInt("minutos", min);
		editor.putInt("raio", r);
		editor.commit();
		finish();
	}

}
